<?php

namespace RHX\Controller;

class TransportController extends RHXController {

    public function indexAction() {
	$transportList = $this->getService('Table\TransportTable')
		->getAll();

	return array(
	    'transportList' => $transportList,
	);
    }

    /**
     * Ajouter un nouveau titre de transport
     * 
     * @return \Zend\View\Model\JsonModel
     */
    public function addAction() {
	$request	 = $this->getRequest();
	$transport	 = new \RHX\Model\Object\Transport($request->getPost());
	$tableTransport	 = new \RHX\Model\Table\TransportTable($this->getAdapter());
	$id		 = $tableTransport->save($transport);
	$transport->id	 = $id;

	return new \Zend\View\Model\JsonModel(array(
	    'transport'	 => $transport,
	    'done'		 => ($id > 0) ? true : false,
	));

	return $this->redirect()->toRoute('RHX/default', array('controller'	 => 'transport', 'action'	 => 'index'));
    }

    public function editAction() {
	$id = (int) $this->params()->fromRoute('id', 0);
	if ($id == 0)
	    return $this->redirect()->toRoute('RHX/default', array('controller'	 => 'site', 'action'	 => 'errorsite', 'id'		 => $id));
	try {
	    $transport = $this->getService('Table\TransportTable')
		    ->get($id);
	}
	catch (\Exception $ex) {
	    return $this->redirect()->toRoute('RHX/default', array('controller'	 => 'site', 'action'	 => 'errorsite', 'id'		 => $id));
	}
	$request = $this->getRequest();
	if ($request->isPost()) {
	    $transport->exchangeArray($request->getPost());
	    $this->getService('Table\TransportTable')
		    ->save($transport);
	    return $this->redirect()->toRoute('RHX/default', array('controller'	 => 'site', 'action'	 => 'success'));
	}
	$this->layout('layout/ajax');

	return array(
	    'id' => $id,
	);
    }

    /**
     * Sauvegarde des données numériques
     *
     * @return \Zend\View\Model\JsonModel
     */
    public function saveIntDataAction() {
	// changement de layout
	$this->layout('layout/ajax');
	// post
	$post	 = $this->getRequest()
		->getPost();
	// variables
	$name	 = $post->name;
	$pk	 = $post->pk;
	$value	 = $post->value;
	// requete
	$sql	 = "UPDATE rh_transport SET $name = $value WHERE id = $pk";
	$result	 = $this->getAdapter()
		->query($sql)
		->execute();
	$data	 = array('done'	 => false, 'error'	 => null);
	if ($result) {
	    $data['done'] = true;
	}
	else {
	    $data['error'] = mysql_error();
	}

	return new \Zend\View\Model\JsonModel($data);
    }

    /**
     * Sauvegarde des données alpha numériques
     * 
     * @return \Zend\View\Model\JsonModel
     */
    public function saveStringDataAction() {
	// changement de layout
	$this->layout('layout/ajax');
	// post
	$post	 = $this->getRequest()
		->getPost();
	// variables
	$name	 = $post->name;
	$pk	 = $post->pk;
	$value	 = addslashes($post->value);
	// requete
	$sql	 = "UPDATE rh_transport SET $name = '$value' WHERE id = $pk";
	$result	 = $this->getAdapter()
		->query($sql)
		->execute();
	$data	 = array('done'	 => false, 'error'	 => null);
	if ($result) {
	    $data['done'] = true;
	}
	else {
	    $data['error'] = mysql_error();
	}

	return new \Zend\View\Model\JsonModel($data);
    }

    /**
     * Effacer un titre de transport
     * 
     * @return \Zend\View\Model\JsonModel
     */
    public function eraseAction() {
	// post
	$post		 = $this->getRequest()
		->getPost();
	// table transport
	$tableTransport	 = new \RHX\Model\Table\TransportTable($this->getAdapter());
	$nb		 = $tableTransport->erase($post->id);

	return new \Zend\View\Model\JsonModel(array(
	    'done'	 => ($nb > 0) ? true : false,
	    'nb'	 => $nb,
	));
    }

}

